package sync;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;




public class Processor {
	
	private final Map<String, Callable> processors=new ConcurrentHashMap<String, Callable>();
	private final ExecutorService executor = Executors.newCachedThreadPool();
	
	
	public Future<String> getFuture(String request) {
		System.out.println("getfuture");
//        if (null==request || isResponse(request) || !isNeedAnswer(request)) return Writeable.EMPTY_FUTURE;
//        else {
            Callable cll = new Callable(this,request);
//            processors.put(request, cll);
            return executor.submit(cll);
//        }
    }


	class Callable implements java.util.concurrent.Callable<String>{
		
		private final String request;
		private String response;

		private final Processor processor;
		
		public Callable(Processor processor,String request) {
			this.processor=processor;
			this.request=request;
			Cache.request=this.request;
		}
		
		
		
//		public void setResult(String response){
//			this.response=response;
//			if(null!=response){
//				synchronized (this) {
//					notifyAll();
//				}
//			}
//		}
		
		@Override
		public String call() throws Exception {
			System.out.println("call");
			if(Cache.response==null){
				synchronized (this) {
					wait(1000);
				}
			}
//			processor.drop(request);
			return Cache.response;
		}
		
	}
	

}
